//
//  MainViewController.m
//  项目三
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController () {

    UIViewController *_currentVc;
    NSString *_selectVcName;
}
@property (nonatomic, strong)UIViewController *currentVc;
@property (nonatomic, copy) NSArray * viewControllers;

@property (nonatomic, strong) UIView * childControllerContainerView;

@property (nonatomic, assign) NSUInteger selectedControllerIndex;

- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation MainViewController
#pragma mark - init
- (instancetype)init {

    self = [super init];
    if (self) {
        self.view.frame = DHFlexibleFrame(CGRectMake(60, 0, ORIGIN_WIDTH - 60, ORIGIN_HEIGHT - 20));
    }
    return self;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
- (void)initializeUserInterface {

    // 默认加载第一个controller的内容
    NSString *currentVcName = CONTROLLERARRAY[0];
    Class currentClass = NSClassFromString(currentVcName);
    UIViewController *currentController = [[currentClass alloc] init];
    _currentVc = currentController;
    [self addChildViewController:currentController];
    
    [self.view addSubview:self.childControllerContainerView];
}
#pragma mark - private methods
- (void)selectControllerAtIndex:(NSUInteger)index {
    
    if (_currentVc) {
        
        [_currentVc.view removeFromSuperview];
        [_currentVc removeFromParentViewController];
    }
#warning
    if ([UserModel defaultUser].isLogin == NO) {
        _selectVcName = CONTROLLER[index];
    } else {
        _selectVcName = CONTROLLERARRAY[index];
    }
    Class selectClass = NSClassFromString(_selectVcName);
    UIViewController *selectedController = [[selectClass alloc] init];
    // 加载选择的controller

    [self addChildViewController:selectedController];

    self.currentVc = selectedController;

}
#pragma mark - override
- (void)addChildViewController:(UIViewController *)childController {
    [self.childControllerContainerView addSubview:childController.view];
    [super addChildViewController:childController];
    [childController didMoveToParentViewController:self];
}
#pragma mark - getter

- (UIView *)childControllerContainerView {
    if (!_childControllerContainerView) {
        _childControllerContainerView = ({
            UIView * view = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, ORIGIN_WIDTH - 60, ORIGIN_HEIGHT - 20))];
            view;
        });
    }
    return _childControllerContainerView;
}
@end
